<template>
  <div class="execution">
    <basic-container>
      <el-card class="operate-container" shadow="never">
        <el-button
          class="btn-add"
          icon="el-icon-plus"
          type="primary"
          @click="handleCreateProduct()"
          size="mini"
        >
          添加
        </el-button>
      </el-card>
      <div class="table-container" style="margin-top: 10px">
        <el-table
          :data="list"
          border
          :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
          v-loading="listLoading"
        >
          <el-table-column
            label="创建时间"
            prop="createTime"
            align="center"
          ></el-table-column>
          <el-table-column
            label="商品ID"
            prop="productId"
            align="center"
          ></el-table-column>
          <el-table-column label="商品缩略图" align="center">
            <template slot-scope="scope">
              <img
                :src="scope.row.productImg"
                style="width: 50px; height: 50px"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="商品名称"
            prop="productName"
            align="center"
          ></el-table-column>
          <el-table-column
            label="价格"
            prop="productPrice"
            align="center"
          ></el-table-column>
          <el-table-column label="排序" align="center">
            <template slot-scope="{ row }">
              <template v-if="row.edit">
                <el-input
                  v-model="row.sort"
                  class="edit-input"
                  size="small"
                  style="width: 30%"
                />
                <el-button
                  class="cancel-btn"
                  size="small"
                  icon="el-icon-refresh"
                  type="warning"
                  @click="cancelEdit(row)"
                  >取消</el-button
                >
              </template>
              <span v-else>{{ row.sort }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作人"
            align="center"
            prop="editName"
          ></el-table-column>
          <el-table-column
            label="操作时间"
            align="center"
            prop="updateTime"
          ></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="{ row }">
              <el-button
                v-if="row.edit"
                type="success"
                size="small"
                icon="el-icon-circle-check-outline"
                @click="confirmEdit(row)"
              >
                确定
              </el-button>
              <el-button
                size="mini"
                type="success"
                @click="row.edit = !row.edit"
                plain
                >编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(row)"
                plain
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="listQuery.size"
          :page-sizes="[5, 10, 15]"
          :current-page.sync="listQuery.current"
          :total="total"
        >
        </el-pagination>
      </div>

      <el-dialog title="添加商品" :visible.sync="dialogVisible" width="40%">
        <el-form :model="passListQuery" ref="passListQuery" label-width="150px">
          <el-form-item label="搜索商品：">
            <el-input
              v-model="keyword"
              style="width: 40%"
              size="small"
            ></el-input>
            <el-button type="primary" @click="hanldSeach()" size="small"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
        <div class="table-container">
          <el-table
            :data="passList"
            style="width: 100%"
            border
            :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
            @selection-change="handleSelectionChange"
            v-loading="listLoading"
          >
            <el-table-column
              type="selection"
              width="60"
              align="center"
              :selectable="checkboxT"
            ></el-table-column>
            <el-table-column label="商品缩略图" align="center">
              <template slot-scope="scope">
                <img :src="scope.row.pic" style="width: 50px; height: 50px" />
              </template>
            </el-table-column>
            <el-table-column
              label="商品名称"
              prop="title"
              align="center"
            ></el-table-column>
            <el-table-column
              label="价格"
              prop="price"
              align="center"
            ></el-table-column>
          </el-table>
        </div>

        <div
          style="
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 20px;
          "
        >
          <div class="batch-operate-container">
            <el-button
              class="search-button"
              @click="handleBatchOperate()"
              type="primary"
              size="small"
            >
              确定
            </el-button>
          </div>
          <div>
            <el-pagination
              background
              @size-change="handleListSizeChange"
              @current-change="handleListCurrentChange"
              layout="total,sizes,prev,pager,next,jumper"
              :page-size="passListQuery.size"
              :page-sizes="[5, 10, 15]"
              :current-page.sync="passListQuery.current"
              :total="passTotal"
            >
            </el-pagination>
          </div>
        </div>
      </el-dialog>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import {
  operateDelLabel,
  operateDelLabelList,
  operateSortDelLabel,
  operateAddDelLabel,
  getProduct,
} from "@/api/home/banner";

export default {
  data() {
    return {
      listLoading: true,
      list: [],
      total: 0,
      listQuery: {
        labelId: null,
        current: 1,
        size: 10,
      },
      dialogVisible: false,
      passList: [],
      passListQuery: {
        current: 1,
        size: 10,
        appId: null,
        labelId: null,
      },
      keyword: null,
      passTotal: 0,
      productIdList: [],
    };
  },
  created() {
    this.listQuery.labelId = this.$route.query.labelId;
    this.passListQuery.labelId = this.$route.query.labelId;
    this.passListQuery.appId = this.$route.query.appId;
    this.getList();
  },
  methods: {
    getList() {
      operateDelLabelList(this.listQuery).then((res) => {
        this.listLoading = false;
        const items = res.data.data.records;
        this.list = items.map((v) => {
          this.$set(v, "edit", false);
          v.originalTitle = v.sort;
          return v;
        });
        this.total = res.data.data.total;
      });
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getList();
    },
    checkboxT(row) {
      if (this.list.length >= 1) {
        for (let i = 0; i < this.list.length; i++) {
          if (this.list[i].productId == row.id) {
            return false;
          } else {
            return true;
          }
        }
      } else {
        return true;
      }
    },
    handleCreateProduct() {
      this.dialogVisible = true;
      this.getPassList();
    },
    hanldSeach() {
      this.passListQuery.current = 1;
      this.passListQuery.keyword = this.keyword;
      this.getPassList();
    },
    getPassList() {
      getProduct(this.passListQuery).then((response) => {
        this.listLoading = false;
        this.passList = response.data.data.records;
        this.passTotal = response.data.data.total;
      });
    },
    handleListSizeChange(val) {
      this.passListQuery.size = val;
      this.getPassList();
    },
    handleListCurrentChange(val) {
      this.passListQuery.current = val;
      this.getPassList();
    },
    handleDelete(row) {
      this.$confirm("是否要删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        operateDelLabel(row.id, this.listQuery.tagId).then(
          (response) => {
            this.$message({
              message: "删除成功",
              type: "success",
              duration: 1000,
            });
            this.getList();
          }
        );
      });
    },
    cancelEdit(row) {
      row.sort = row.originalTitle;
      row.edit = false;
      this.$message({
        message: "已恢复到原来的价值",
        type: "warning",
      });
    },
    confirmEdit(row) {
      row.edit = false;
      row.originalTitle = row.sort;
      let params = {
        id: row.id,
        sort: row.sort,
      };
      operateSortDelLabel(params).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: res.data.msg,
            type: "success",
            duration: 1000,
          });
          this.getList();
        } else {
          this.$message({
            message: res.data.msg,
            type: "warning",
            duration: 1000,
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.productIdList = val;
    },
    handleBatchOperate() {
      if (this.productIdList < 1) {
        this.$message({
          message: "请选择一条记录",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let ids = [];
      for (let i = 0; i < this.productIdList.length; i++) {
        ids.push(this.productIdList[i].id);
      }
      this.handleAddProductAttr(ids);
    },
    handleAddProductAttr(ids) {
      let params = {
        labelId: this.listQuery.labelId,
        productIds: ids,
      };
      operateAddDelLabel(params).then((res) => {
        this.dialogVisible = false;

        this.passListQuery.title = null;
        if (res.data.code === 200) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.getList();
        } else {
          this.$message({
            message: res.data.msg,
            type: "warning",
          });
        }
      });
    },
  },
};
</script>
